<?php

namespace common\models;

use Yii;
use common\extended\system\Util;

/**
 * This is the model class for table "bus_member_consume".
 *
 * @property string $consume_id
 * @property string $member_id
 * @property string $consume_time
 * @property string $consume_amount
 * @property string $consume_name
 * @property string $shop_id
 * @property string $consume_type
 * @property string $cash_pay
 * @property string $unionpay
 * @property string $balance_pay
 * @property string $give_pay
 * @property string $debt_count
 * @property string $boss_id
 */
class BusMemberConsume extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'bus_member_consume';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['member_id', 'consume_amount', 'consume_name', 'shop_id', 'consume_type', 'debt_count'], 'required'],
            [['member_id', 'shop_id', 'boss_id'], 'integer'],
            [['consume_time'], 'safe'],
            [['consume_amount', 'cash_pay', 'unionpay', 'balance_pay', 'give_pay', 'debt_count'], 'number'],
            [['consume_name'], 'string', 'max' => 255],
            [['consume_type'], 'string', 'max' => 45]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'consume_id' => 'Consume ID',
            'member_id' => 'Member ID',
            'consume_time' => 'Consume Time',
            'consume_amount' => 'Consume Amount',
            'consume_name' => 'Consume Name',
            'shop_id' => 'Shop ID',
            'consume_type' => 'Consume Type',
            'cash_pay' => 'Cash Pay',
            'unionpay' => 'Unionpay',
            'balance_pay' => 'Balance Pay',
            'give_pay' => 'Give Pay',
            'debt_count' => 'Debt Count',
            'boss_id' => 'Boss ID',
        ];
    }
    /**
     * 统计今日(本月)到店人数、消耗金额、现金、刷卡
     * @date: 2016年3月11日 
     * @author: chenxiaolin
     */
    public function statisticsMoney($type,$shop_id){
        $data = [];
        if($type == 1){//今日
            $date = date('Y-m-d');
            $consume = BusMemberConsume::find()->where("shop_id = :id and consume_time like '".$date."%'",[':id' => $shop_id]);
            //今日现金
            $todayCash = $consume->sum('cash_pay');
            if(empty($todayCash)){
                $todayCash = 0;
            }
            //今日刷卡
            $todayUnion = $consume->sum('unionpay');
            if(empty($todayUnion)){
                $todayUnion = 0;
            }
            //今日到店人数
            $todayMember = BusService::find()->where("shop_id = :id and service_status = 2 and service_addtime like '".$date."%'",[':id' => $shop_id])->count();
            if(empty($todayMember)){
                $todayMember = 0;
            }
            //今日消耗
            $todayConsume = BusEmployeeReward::find()->where("shop_id = :id and create_time like '".$date."%'",[':id' => $shop_id])->sum('consume_reward');
            if(empty($todayConsume)){
                $todayConsume = 0;
            }
            $data = [
                'cash' => $todayCash,
                'union' => $todayUnion,
                'member' => $todayMember,
                'consume' => $todayConsume,
            ];
            return $data;
        }
        if($type == 2){//本月
           //获取本月时间
           $month = Util::getMonth(date('Y-m-d',time()));
           $time = explode('至', $month);
           $startTime = $time[0];
           $endTime = $time[1];
           $consumeQuery = BusMemberConsume::find()->where("shop_id = :id and consume_time >= '".$startTime."' and consume_time <= '".$endTime."'",[':id' => $shop_id]);
           //本月现金
           $monthCash = $consumeQuery->sum('cash_pay');
           if(empty($monthCash)){
               $monthCash = 0;
           }
           //本月刷卡
           $monthUnion = $consumeQuery->sum('unionpay');
           if(empty($monthUnion)){
               $monthUnion = 0;
           }
           //本月到店人数
           $monthMember = BusService::find()->where("shop_id = :id and service_status = 2 and service_addtime >= '$startTime' and service_addtime <= '$endTime'",[':id' => $shop_id])->count();
           if(empty($monthMember)){
               $monthMember = 0;
           }
           //本月到消耗
           $monthConsume = BusEmployeeReward::find()->where("shop_id = :id and create_time >= '$startTime' and create_time <= '$endTime'",[':id' => $shop_id])->sum('consume_reward');
           if(empty($monthConsume)){
               $monthConsume = 0;
           }
           $data = [
                'cash' => $monthCash,
                'union' => $monthUnion,
                'member' => $monthMember,
                'consume' => $monthConsume,
            ];
            return $data;
        }
    }

    /**
     * 获取会员的累计消费金额
     * @param int $member_id 会员id
     * @return mixed
     */
    public static function getConsumeAll($member_id)
    {
        $sumConsumeAmount = self::find()->select(['sum(cash_pay) sum_cash_pay', 'sum(unionpay) sum_unionpay'])->where(['member_id' => $member_id])->andWhere(['!=', 'consume_name', '退款'])->asArray()->one();
        return ($sumConsumeAmount['sum_cash_pay'] + $sumConsumeAmount['sum_unionpay']) ?: '0.00';
    }
}
